//
//  LeHuViewController.m
//  werfgh
//
//  Created by 李晓勇 on 14-9-21.
//  Copyright (c) 2014年 dingshenwangluokeji. All rights reserved.
//

#import "LeHuViewController.h"
#import "AFNetworking.h"
#import "Header.h"
@interface LeHuViewController ()<UITextFieldDelegate>
{
    UITextField*field1;
    UITextField*field2;
    UILabel *label2;
    UILabel *label1;
    UIAlertView*_alertView;
}


@end

@implementation LeHuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:246.0/255 green:247.0/255 blue:248.0/255 alpha:1] ;
    if (IOS7) {
        self.edgesForExtendedLayout=NO;
    }
    self.title=self.str2;
    label1=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
    label2=[[UILabel alloc]initWithFrame:CGRectMake(10, 35, 300, 30)];
    label1.text=[NSString stringWithFormat:@"账 号 : %@",self.str1 ];
    label2.text=[NSString stringWithFormat:@"套 餐 : %@",self.str];
    label2.font=[UIFont systemFontOfSize:19];
    label1.font=[UIFont systemFontOfSize:19];
//    [self.view addSubview:label1 ];
//    [self.view addSubview:label2 ];
    
    
    
    
    UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(10, 90, 300, 40)];
    
    [self.view addSubview:label3];
    
    field1=[[UITextField alloc]initWithFrame:CGRectMake(10, 130, 300, 40)];
    field1.background=[UIImage imageNamed:@"beijingkuang2"];
    field1.keyboardType=UIKeyboardTypeNumberPad;
    label3.text=[NSString stringWithFormat:@"充值卡序列号"];
    field1.delegate=self;
    [self.view addSubview:field1];
    
    UITextField* field=[[UITextField alloc]initWithFrame:CGRectMake(10, 170, 300, 40)];
    UILabel*label4=[[UILabel alloc]initWithFrame:CGRectMake(10, 170, 300, 40)];
    //field.background=[UIImage imageNamed:@"beijingkuang2"];
    [self.view addSubview:field];
    
    label4.text=[NSString stringWithFormat:@"充值卡密码"];
    [self.view addSubview:label4];
    field2=[[UITextField alloc]initWithFrame:CGRectMake(10, 210, 300, 40)];
    field2.background=[UIImage imageNamed:@"beijingkuang2"];
    field2.delegate=self;
    
    field2.keyboardType=UIKeyboardTypeNumberPad;
    [self.view addSubview:field2];
    UITextField* field3=[[UITextField alloc]initWithFrame:CGRectMake(10, 250, 300, 40)];
    // field3.background=[UIImage imageNamed:@"beijingkuang2"];
    [self.view addSubview:field3];
    field3.userInteractionEnabled=NO;
    field.userInteractionEnabled=NO;
    field.backgroundColor=[UIColor colorWithRed:246.0/255 green:247.0/255 blue:248.0/255 alpha:1] ;
    field3.backgroundColor=[UIColor colorWithRed:246.0/255 green:247.0/255 blue:248.0/255 alpha:1] ;
    // field3.backgroundColor=[UIColor redColor];
    UIButton*btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(50, 270, 220, 40);
    [btn setTitle:@"确认充值" forState:UIControlStateNormal];
    btn.titleLabel.textAlignment=1;
    //[btn setBackgroundImage:[UIImage imageNamed:@"queren.png"] forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor blueColor];
    [btn addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    // Do any additional setup after loading the view.
}
-(void)tap
{
    NSString*str11;
    if ([self.str2 isEqualToString:@"中国移动"])
    {
        str11=@"SZX";
    }
    else if ([self.str2 isEqualToString:@"中国联通"]) {
        str11=@"UNICOM";
    }
    else
    {
        str11=@"TELECOM";
        
    }
    NSLog(@"%@",str11);
    
    NSString*str12;
    if ([self.str rangeOfString:@"30"].location!=NSNotFound)
    {
        str12=@"30";
    }
    else if([self.str rangeOfString:@"50"].location!=NSNotFound)
    {
        str12=@"50";
    }
    else
    {
        str12=@"100";
    }
    
    if ([field2.text isEqualToString:@""]||[field1.text isEqualToString:@""])
    {
        
        _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"充值卡序列号或者密码为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [_alertView show];
        return;
        
    }
    else{
        
        
        NSString *str=[NSString stringWithFormat:@"http://oemapi2.leehuu.cn/api/client/UsePrepaidCard?userName=%@&cardNumber=%@&cardPassword=%@",self.str,field1.text,field2.text];
        NSLog(@"%@ %@ %@ ",self.str,field1.text,field2.text);
        NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *html = operation.responseString;
            NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
            id dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
            NSLog(@"获取到的数据为1：%@",dict);
            
            NSString* excString=[dict objectForKey:@"exception"];
            NSString* retString=[dict objectForKey:@"retCode"];
            NSLog(@"1%@",excString);
            NSLog(@"2%@",retString);
            NSString *message = @"数据已提交，请稍后查询余额";
            if ([retString intValue])
            {
                
                message=excString;
                _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                
                [_alertView show];
                return;
            }
            else
                //信息正确
            {
                _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                
                [_alertView show];
                
            }
            return;
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                             NSLog(@"发生错误！%@",error);
                                         }];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:operation];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [field2 resignFirstResponder];
    [field1 resignFirstResponder];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    
    return YES;
}
//键盘上下移动
-(void)textFieldDidBeginEditing:(UITextField *)textField{   //开始编辑时，整体上移
    //    if (textField.tag==0) {
    //        [self moveView:-20];
    //    }
    //    if (textField.tag==1)
    //    {
    [self moveView:-40];
    //    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{     //结束编辑时，整体下移
    //    if (textField.tag==0) {
    //        [self moveView:20];
    //    }
    //    if (textField.tag==1)
    //    {
    [self moveView:40];
    //    }
}
-(void)moveView:(float)move{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    frame.origin.y+=move;//view的X轴上移
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];//设置调整界面的动画效果
    
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
